{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7f0391bc",
   "metadata": {},
   "source": [
    "* 本学习记录人：陈依玟\n",
    "\n",
    "* 学习时间：2021-11-23\n",
    "\n",
    "* week13\n",
    "\n",
    "* 参考的文档：\n",
    "\n",
    "> 1. [Python的官方文档]（https://docs.python.org/3/）\n",
    "> 2. [许老师的博客]（https://www.cnblogs.com/xuzhichao/p/11407192.html）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81e23b57",
   "metadata": {},
   "source": [
    "# 本周重点知识\n",
    "## 1. 初始字典\n",
    "## 2.字典的一般创建方法\n",
    "## 3.字典的操作\n",
    "## 4.实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e0467be",
   "metadata": {},
   "source": [
    "# 1.初始[字典]()\n",
    "\n",
    "* Another useful data type built into python is dictionary (see Mapping Types --dict).Dictionaries are sometimes\\\n",
    "* 对比序列数据 VS 映射数据\n",
    ">> 1. 序列数据 index 是 从 0 开始，每次加1的数值作为索引值[88,87,89]\n",
    ">> 2. 映射数据 index 是**关键字** （不可变数据，例如字符串，数字，元组）\n",
    ">> 3. 序列数据是**有顺序的**\n",
    ">> 4. 映射数据是**无序的**\n",
    ">> 5. 序列数据 index 是不写在序列中的 不显示的\n",
    ">> 6. 映射数据 index 作为 关键字写在数据结构中 ；{key：value} 这种形式通常被人们称为**键值对(key:value pairs)**\n",
    "\n",
    "* 总结字典的性质:\n",
    "> 1. 字典的key是唯一的（唯一性）\n",
    "> 2. 字典是没有顺序的（无序性）\n",
    "> 3.字典的key是不可变的（不变性）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b1c9b52d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始字典\n",
    "tel = {'Jack':4098,'sape':4139}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "92367c92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Jack': 4098, 'sape': 4139}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "107e118e",
   "metadata": {},
   "source": [
    "# 2.字典的一般创建方法\n",
    "\n",
    "> 1. 按照字典的数据结构直接创建\n",
    ">> * dict_name = {key1:value1,key2:value2,key3,value3,...}\n",
    "> 2. 使用dict()方法创建字典\n",
    ">> * dict_name = dict(key1=value1,key2=value2,key3=value3,...)\n",
    ">3. 使用字典新增的方式创建字典\n",
    ">> * dict_name[key1] = value1 , dict_name[key2] = value2, dict_name[key3] = value3 ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "257bdaa9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# **注意 ： 冒号和逗号都是英文的**\n",
    "stud_info = {\n",
    "    \"2100001\":\"Alex\",\n",
    "    \"2100002\":\"Jack\",\n",
    "    \"2100003\":\"Lihua\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8519f8f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'Lihua'}\n"
     ]
    }
   ],
   "source": [
    "print(type(stud_info))\n",
    "print(stud_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "035ba8a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info[\"2100002\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7eab58e2",
   "metadata": {},
   "source": [
    "· 练习查询 https://httpbin.org/get"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "cbf88d52",
   "metadata": {},
   "outputs": [],
   "source": [
    "get_diet = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61a590f5-6e5b90697433f1625281f83b\"\n",
    "  }, \n",
    "  \"origin\": \"61.242.54.238\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "b69d31b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       " 'Accept-Encoding': 'gzip, deflate',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9',\n",
       " 'Host': 'httpbin.org',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',\n",
       " 'X-Amzn-Trace-Id': 'Root=1-61a590f5-6e5b90697433f1625281f83b'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_diet[\"headers\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e96d7d56",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  使用dict()方法创建字典(局限性： key 是以 变量名的方式进行赋值的)\n",
    "stud_info2 = dict(\n",
    "    name1 = \"Alex\",\n",
    "    name2 = \"Jack\",\n",
    "    name3 = \"Lihua\"\n",
    "    \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a71c12be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name1': 'Alex', 'name2': 'Jack', 'name3': 'Lihua'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "43736a49",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 使用字典新增的方式创建字典\n",
    "stud_info3 = { }\n",
    "stud_info3[\"2100001\"] = \"Alex\"\n",
    "stud_info3[\"2100002\"] = \"Jack\"\n",
    "stud_info3[\"2100003\"] = \"Lihua\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3ad34385",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'Lihua'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36da3182",
   "metadata": {},
   "source": [
    "# 3.字典的操作\n",
    "\n",
    "> 1. 字典的增删改查\n",
    ">> 1. 字典的查询\n",
    ">> 2. 字典的增加\n",
    ">> 3. 字典的修改\n",
    ">> 4. 字典的删除\n",
    "> 2. 字典的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c2d3c89",
   "metadata": {},
   "source": [
    "> 1. 查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3023285c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2100004不存在\n"
     ]
    }
   ],
   "source": [
    "# 字典的查询 方法1\n",
    "if \"2100004\" in stud_info3:\n",
    "    print(stud_info3[\"2100004\"])\n",
    "else:\n",
    "    print(\"2100004\"+\"不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "772a38f2",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'2100004'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-189e82219ce9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 如果查询不存在 : KeyError : '2100004'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstud_info3\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'2100004'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: '2100004'"
     ]
    }
   ],
   "source": [
    "# 如果查询不存在 : KeyError : '2100004'\n",
    "print(stud_info3['2100004'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d8337c01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# 1.字典的查询 方法2 get()\n",
    "print(stud_info3.get('2100004'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7c024767",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.字典的查询  方法2\n",
    "stud_info3.get('2100002')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69313eef",
   "metadata": {},
   "source": [
    "> 2.字典的增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "30cf3ab4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stud_info3.get(\"2100004\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "09a1a372",
   "metadata": {},
   "outputs": [],
   "source": [
    "if stud_info3.get(\"2100004\") == None:\n",
    "    stud_info3[\"2100004\"] = \"Mary\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "906d54c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'Lihua', '2100004': 'Mary'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f61c2553",
   "metadata": {},
   "source": [
    "> 3.字典的改变\n",
    ">> 1.dict_name[key] = value\n",
    ">> 2.注意： 如果字典key不存在，则新增；如果key存在，则修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "1bfacc3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"2100004\"in stud_info3:\n",
    "    stud_info3[\"2100004\"] = \"Xiaoming\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "838671be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'Lihua'}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84d53a4f",
   "metadata": {},
   "source": [
    "> 4. 字典的删除\n",
    ">> 1. pop()\n",
    ">>> 1. D.pop(k[,d])\n",
    ">>> 2. remove specified key (指定的key关键字) and return the corresponding value(返回删除的value值)\n",
    ">> 2. popitem():随机删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "cf5b0784",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'Lihua', '2100004': 'Mary'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0989b3fb",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'stud_info3' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-9e9d9f4c56dd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstud_info3\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'stud_info3' is not defined"
     ]
    }
   ],
   "source": [
    "help(stud_info3.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "20f0c877",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Lihua'"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 因为字典是无序的，所以没有最后一个元素，pop指定关键字删除\n",
    "stud_info3.pop(\"2100003\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "652ea11c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack'}"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01c49cf0",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(stud_info3.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "4ea3971c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('2100002', 'Jack')"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b647b65",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
